﻿namespace Admin.NET.Application.DL645
{
    /// <summary>
    /// 参数
    /// </summary>
    public static class DL645ParameterSet
    {
        private static List<DL645Parameter> DLparameter = new List<DL645Parameter>()
        {
            //new DL645Parameter(){Key="33333433",Company="kWh",Count=8,Name="正向电量",RetainCount=2,RealKey="00000100" },
            new DL645Parameter(){Id=1, Key="3334C335",IsRead=true,RealKey="00019002",Count=4,Name="当前剩余电流值",RetainCount=4,Company="mA" },
            new DL645Parameter(){Id=2, Key="33323435",IsRead=true,RealKey="00FF0102",Count=4,Name="当前A、B、C三相电压值",RetainCount=3,Company="Kw"  },
            new DL645Parameter(){Id=3,Key="3334C435",IsRead=true,RealKey="00019102",Count=4,Name="当前额定剩余电流值",RetainCount=4,Company="mA" },
            new DL645Parameter(){Id=4,Key="33323535",IsRead=true,RealKey="00FF0202",Count=6,Name="当前A、B、C三相电流",RetainCount=3,Company="A" },
            new DL645Parameter(){Id=5,Key="36373337",IsRead=false,RealKey="03040004",Count=64,Name="资产管理编号",RetainCount=64,Company="" },
            new DL645Parameter(){Id=6,Key="35433337",IsRead=false,RealKey="02100004",Count=2,Name="控制字2",RetainCount=2,Company="" },
            new DL645Parameter(){Id=7,Key="37433337",IsRead=false,RealKey="04100004",Count=2,Name="控制字4",RetainCount=2,Company="" },
            new DL645Parameter(){Id=8,Key="34383337",IsRead=false,RealKey="01050004",Count=2,Name="运行状态字1",RetainCount=2,Company="" },
            new DL645Parameter(){Id=9,Key="34473337",IsRead=false,RealKey="01140004",Count=6,Name="额定电流整定值过载保护动作电流Ir1",RetainCount=5,Company="" },
            new DL645Parameter(){Id=10,Key="36473337",IsRead=false,RealKey="03140004",Count=2,Name="过载保护动作时间t1",RetainCount=2,Company="" },
            new DL645Parameter(){Id=11,Key="36433337",IsRead=false,RealKey="03100004",Count=2,Name="控制字3",RetainCount=2,Company="" },
            new DL645Parameter(){Id=12,Key="38473337",IsRead=false,RealKey="05140004",Count=4,Name="短路短延时动作时间t2",RetainCount=4,Company="" },
            new DL645Parameter(){Id=13,Key="37473337",IsRead=false,RealKey="04140004",Count=2,Name="短路短延时动作电流Ir2 (*Ir1)",RetainCount=2,Company="" },
            new DL645Parameter(){Id=14,Key="39473337",IsRead=false,RealKey="06140004",Count=2,Name="短路瞬时电流Ir3 (*Ir1)",RetainCount=2,Company="" },
            new DL645Parameter(){Id=15,Key="34463337",IsRead=false,RealKey="01130004",Count=4,Name="过电压整定值",RetainCount=3,Company="" },
            new DL645Parameter(){Id=16,Key="35463337",IsRead=false,RealKey="02130004",Count=4,Name="欠电压整定值",RetainCount=3,Company="" },
            new DL645Parameter(){Id=17,Key="36463337",IsRead=false,RealKey="03130004",Count=4,Name="断相电压整定值",RetainCount=3,Company="" },
            new DL645Parameter(){Id=18,Key="32463337",IsRead=false,RealKey="FF130004",Count=4,Name="电压整定参数块",RetainCount=3,Company="" },
            new DL645Parameter(){Id=19,Key="32473337",IsRead=false,RealKey="FF140004",Count=18,Name="电流整定参数块",RetainCount=18,Company="" },
            new DL645Parameter(){Id=20,Key="34433337",IsRead=false,RealKey="01100004",Count=2,Name="控制字1",RetainCount=2,Company="" },
            new DL645Parameter(){Id=21,Key="33333333",IsRead=false,RealKey="00000000",Count=8,Name="组合有功电能数据块",RetainCount=6,Company="" },
            new DL645Parameter(){Id=22,Key="33323735",IsRead=false,RealKey="00FF0402",Count=6,Name="瞬时无功功率数据块",RetainCount=2,Company="" },
            new DL645Parameter(){Id=23,Key="33323835",IsRead=false,RealKey="00FF0502",Count=6,Name="瞬时视在功率数据块",RetainCount=2,Company="" },
            new DL645Parameter(){Id=24,Key="33333633",IsRead=false,RealKey="00000300",Count=8,Name="组合无功 1 总电能",RetainCount=6,Company="" },
            new DL645Parameter(){Id=25,Key="33333433",IsRead=false,RealKey="00000100",Count=8,Name="正向有功总电能",RetainCount=6,Company="" },
            new DL645Parameter(){Id=26,Key="33333533",IsRead=false,RealKey="00000200",Count=8,Name="反向有功总电能",RetainCount=6,Company="" },
            new DL645Parameter(){Id=27,Key="33323635",IsRead=false,RealKey="00FF0302",Count=6,Name="瞬时有功功率数据块",RetainCount=2,Company="" },
            new DL645Parameter(){Id=28,Key="39373337",IsRead=false,RealKey="06040004",Count=2,Name="最大（壳架）电流(Inm)",RetainCount=2,Company="" },


            new DL645Parameter(){Id=129,Key="33323333",IsRead=false,RealKey="00FF0000",Count=8,Name="组合有功电能",RetainCount=6,Company="" },
            new DL645Parameter(){Id=130,Key="33323433",IsRead=false,RealKey="00FF0100",Count=8,Name="正向有功总电能",RetainCount=6,Company="" },
            new DL645Parameter(){Id=131,Key="33323533",IsRead=false,RealKey="00FF0200",Count=8,Name="反向有功电能数据块",RetainCount=6,Company="" },
             new DL645Parameter(){Id=133,Key="33323935",IsRead=false,RealKey="00FF0602",Count=4,Name="总功率因数块",RetainCount=4,Company="" },
            new DL645Parameter(){Id=132,Key="36383337",IsRead=false,RealKey="03050004",Count=4,Name="运行状态3",RetainCount=4,Company="" },
            new DL645Parameter(){Id=135,Key="33343333",IsRead=false,RealKey="00010000",Count=8,Name="水表645抄表",RetainCount=6,Company="" }
        };
        /// <summary>
        /// 根据静态字典获取数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="code">1 额定剩余电流动作值 2电流保护 3极限不驱动时间</param>
        /// <returns></returns>
        public static string GetValueByTypeCode(string key, string code)
        {
            return code switch
            {
                "1" => GetRatedCurrent(key),
                "2" => GetCurrentProtect(key),
                "3" => GetLimitTime(key),
                _ => ""
            };
        }

        private static string GetLimitTime(string key)
        {
            return key switch
            {
                "00" => "60",
                "01" => "100",
                "10" => "200",
                _ => "",
            };
        }

        public static string AnalisReading(string infotype, string infoData)
        {

            DL645Parameter parameter =GetDLParameter(infotype);
            if (parameter == null)
            {
                return "";
            }
            Span<string> spandata = new Span<string>(new string[infoData.Length / 2]);
            for (int i = 0, index = 0; i < infoData.Length; i = i + 2, index++)
            {
                spandata[index] = infoData.Substring(i, 2);
            }
            spandata.Reverse();

            //数量
            int count = spandata.Length / (parameter.Count / 2);
            Span<string> realinfo = new Span<string>(new string[count]);
            for (int i = 0; i < count; i++)
            {
                string value = "";
                for (int j = (parameter.Count / 2) * i; j < (parameter.Count / 2) * (i + 1); j++)
                {
                    value += (Convert.ToInt32(spandata[j], 16) - 51).ToString("X2").PadLeft(2, '0');
                }
                if (parameter.Count != parameter.RetainCount)
                {
                    value = value.Insert(parameter.RetainCount, ".");
                }
                realinfo[i] = value;
            }
            return string.Join(',', realinfo.ToArray());
        }
        /// <summary>
        /// 电流保护
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private static string GetCurrentProtect(string key)
        {
            return key switch
            {
                "00" => "0",//不跳闸，不告警
                "01" => "1",//不跳闸，告警
                "10" => "2",//跳闸，不告警
                _ => "",
            };
        }

        public static string GetCurrentProtectByCode(int code)
        {
            return code switch
            {
                0 => "00",//不跳闸，不告警
                1 => "01",//不跳闸，告警
                2 => "10",//跳闸，不告警
                _ => "",
            };
        }
        private static string GetRatedCurrent(string key)
        {
            return key switch
            {
                "0000" => "100",
                "0001" => "200",
                "0010" => "300",
                "0011" => "400",
                "0100" => "500",
                "0101" => "600",
                "0110" => "800",
                "0111" => "1000",

                _ => "",
            };
        }

        /// <summary>
        /// 返回key字符串
        /// </summary>
        /// <returns></returns>
        public static List<DL645Parameter> GetParameterByKey(string parameter = null)
        {
            if (parameter == null)
            {
                return DLparameter;

            }
            else
            {
                return DLparameter.Where(u => u.Key == parameter.ToUpper()).ToList();
            }


        }
        public static string GetParameterById(int Id)
        {
            return DLparameter.Where(u => u.Id == Id).FirstOrDefault()?.Key;
        }
        /// <summary>
        /// 返回指定
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static DL645Parameter GetDLParameter(string key)
        {
            return DLparameter.Where(u => u.Key == key.ToUpper()).FirstOrDefault();
        }
        /// <summary>
        /// 返回指定
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static DL645Parameter GetDLParameter(int Id)
        {
            return DLparameter.Where(u => u.Id == Id).FirstOrDefault();
        }
    }
}
